package com.htfyun.ft.showinfo;

import android.animation.ValueAnimator;
import android.text.TextUtils;

import com.htfyun.ft.FTApplication;
import com.htfyun.ft.R;
import com.htfyun.ft.common.BatteryCurrentThresholdEnum;
import com.htfyun.ft.common.BatteryInfo;
import com.htfyun.ft.common.FTUtils;
import com.htfyun.ft.config.NVRomKeysAttr;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.ft.utils.FileReadWrite;
import com.htfyun.serialport.uartservo.ServoInterestedInfo;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.collection.LruCache;

public enum ShowInfoSingleton {
    INSTANCE
    ;

    private LruCache<ShowInfoItemEnum, ShowInfoBean> showInfoBeanLruCache = new LruCache<>(ShowInfoItemEnum.values().length);

    private static final String SYS_WIFI_MACADDR_PATH_STRING = "/sys/class/net/wlan0/address";

    public ShowInfoBean getShowInfoBean(ShowInfoItemEnum itemEnum, Object extra) {

        ShowInfoBean bean = showInfoBeanLruCache.get(itemEnum);
        if (bean == null) {
            bean = new ShowInfoBean(itemEnum);
            showInfoBeanLruCache.put(itemEnum, bean);
        }

        switch (itemEnum) {

            case battery: {
                if (extra instanceof BatteryInfo) {
                    bean.setTxt(getBatteryInfo((BatteryInfo) extra));
                }
                break;
            }
            case servo: {
                if (extra instanceof ServoInterestedInfo) {
                    bean.setTxt(getServoInfo((ServoInterestedInfo) extra));
                    bean.setResult(ShowInfoResultAttr.PASS);
                } else if (bean.getResult() != ShowInfoResultAttr.PASS) {
                    bean.setResult(ShowInfoResultAttr.FAIL);
                    bean.setTxt(itemEnum.getDefaultInfo());
                }
                break;
            }
            case ft_test: {

                String value = getString(R.string.info_ft_not_over);
                ShowInfoResultAttr result = ShowInfoResultAttr.FAIL;
                if (FTUtils.isFactoryTestItemsAllPassedExcludeNoFragment()) {
                    value = getString(R.string.info_ft_is_over);
                    result = ShowInfoResultAttr.MA;
                }

                bean.setResult(result);
                bean.setTxt(value);

                break;
            }
            case pc_checked: {
                String value = itemEnum.getStringFroNVRom();
                ShowInfoResultAttr result = ShowInfoResultAttr.MA;
                String txt = getString(R.string.pc_unchecked);
                if (!TextUtils.isEmpty(value) && value.equals(NVRomKeysAttr.PC_CHECKED)) {
                    result = ShowInfoResultAttr.PASS;
                    txt = getString(R.string.pc_checked);
                }
                bean.setResult(result);
                bean.setTxt(txt);
                break;
            }
            case wmac: {
                String txt = FileReadWrite.readFileByLines(SYS_WIFI_MACADDR_PATH_STRING);
                ShowInfoResultAttr result = ShowInfoResultAttr.PASS;

                if (TextUtils.isEmpty(txt)) {
                    result = ShowInfoResultAttr.MA;
                    txt = getString(R.string.wifi_not_enabled);
                }
                bean.setResult(result);
                bean.setTxt(txt);
                break;
            }
            case bsn:
            case sn:
            case imei:
            case btmac:
            case user:
            case pcba_result: {
                ShowInfoResultAttr result = ShowInfoResultAttr.PASS;
                String value = itemEnum.getStringFroNVRom();
               if (TextUtils.isEmpty(value)) {
                    value = itemEnum.getDefaultInfo();
                    result = ShowInfoResultAttr.FAIL;
                }

                bean.setResult(result);
                bean.setTxt(value);
                break;
            }
            default:
                break;
        }

        return bean;
    }

    private String getServoInfo(ServoInterestedInfo info) {
        return getString(R.string.servo_show_info,
                info.getAngleAreaMin(),
                info.getAngleAreaMax()
                );
    }
    private String getBatteryInfo(BatteryInfo info) {

        return getString(R.string.battery_info,
                info.getBatteryVoltage(),
                info.getBatteryLevel(),
                info.getBatteryStatusString(),
                info.getBatteryCurrent_mA());
    }

    private boolean isBatteryPassed(BatteryInfo info) {

        BatteryCurrentThresholdEnum thresholdEnum = null;
        for (BatteryCurrentThresholdEnum e : BatteryCurrentThresholdEnum.values()) {
            if (e.isInRange(info.getBatteryLevel())) {
                thresholdEnum = e;
                break;
            }
        }

        if (thresholdEnum == null) {
            return false;
        }

        final int curren_mA = info.getBatteryCurrent_mA();

        return (curren_mA >= thresholdEnum.getCurrent() || curren_mA < 0);

    }


    @NonNull
    public final String getString(@StringRes int resId) {
        return FTApplication.getContext().getString(resId);
    }


    @NonNull
    public final String getString(@StringRes int resId, Object... formatArgs) {
        return FTApplication.getContext().getString(resId, formatArgs);
    }
}
